﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyAI : MonoBehaviour
{
    private float rotation;
    public List<Vector3> pathPoses = new List<Vector3>();
    public float speed = 1;
    private int next = 0;
    private int moveState = 0; //0 未开始 1 移动 2 转向 3暂停
    private float distance = 0;
    private float turnSpeed=100;
    private float alertDirection=-1;
    
    // Use this for initialization
    void Start()
    {
        pathPoses.Add(new Vector3(0, this.transform.position.y, 0));
        pathPoses.Add(new Vector3(2, this.transform.position.y, 2));
        pathPoses.Add(new Vector3(0, this.transform.position.y, 3));
        StartCoroutine(Move());

    }

    // Update is called once per frame
    void Update()
    {
        
        this.transform.rotation = Quaternion.Euler(0, this.transform.rotation.eulerAngles.y, 0);
        if (moveState == 1)
        {
            this.transform.position += (pathPoses[next] - this.transform.position).normalized * speed * Time.deltaTime;
        }
        if (moveState == 2)
        {
            this.transform.Rotate(Vector3.Cross(this.transform.forward, (pathPoses[next] - this.transform.position)).normalized * turnSpeed * Time.deltaTime);
        }
    }
    IEnumerator Move()
    {
        while (true)
        {

            moveState = 2;
            yield return new WaitUntil(() =>Vector3.Angle(this.transform.forward, (pathPoses[next] - this.transform.position).normalized) <= 1);
            moveState = 1;
            yield return new WaitUntil(() => Calculator.Distance(this.transform.position, pathPoses[next]) <= 0.1);
            next++;
            next = next < pathPoses.Count ? next : 0;
        }


    }
    public void FoundPlayer(Vector3 vec)
    {
        StopCoroutine(Move());
        moveState = 3;
        vec.Set(vec.x, this.transform.position.y, vec.z);
        this.transform.LookAt(vec);
        Debug.Log("YOU LOSE!!!");
        Time.timeScale = 0;
    }

    IEnumerator Alert()
    {
        
        moveState = 3;
        var alertTime = 0.0f;
        var alertChangeTime = 0.5f;
        while (true)
        {
            yield return new WaitForEndOfFrame();
            this.transform.Rotate(new Vector3(0,alertDirection,0) * turnSpeed * Time.deltaTime);
            alertTime += Time.deltaTime;
            alertChangeTime += Time.deltaTime;
            if (alertChangeTime >= 1)
            {
                alertChangeTime = 0;
                alertDirection = -alertDirection;
            }
            if (alertTime >= 5)
            {
                break;
            }
        }
        moveState = 2;
    }

    IEnumerator MovetoBody(Vector3 vec)
    {
        var totalTime = Calculator.Distance(this.transform.position, vec) / speed;
        var realTotalTime= (Calculator.Distance(this.transform.position, vec)-1) / speed;
        var startPos = this.transform.position;
        var time = 0.0f;
        while (true)
        {

            yield return new WaitForEndOfFrame();
            time += Time.deltaTime;
            this.transform.position = Vector3.Lerp(startPos,vec, time/totalTime);
            if (time>=realTotalTime)
            {
                
                break;
            }
            
        }
        StartCoroutine(Alert());
        
    }
    public void FoundBody(Vector3 vec)
    {
        if (moveState != 3)
        {

            
            moveState = 3;
            vec.Set(vec.x, this.transform.position.y, vec.z);
            this.transform.LookAt(vec);
            StartCoroutine(MovetoBody(vec));
        }
        
    }
}
